package net.wanho.controller;

import lombok.RequiredArgsConstructor;
import net.wanho.R.R;
import net.wanho.dto.BuyListAddDTO;
import net.wanho.dto.BuyListDTO;
import net.wanho.dto.BuyListInStoreDTO;
import net.wanho.dto.BuyListQueryDTO;
import net.wanho.dto.BuyListUpdateDTO;
import net.wanho.service.BuyListService;
import net.wanho.vo.BuyListExportVO;
import net.wanho.vo.BuyListVo;
import net.wanho.vo.PageInfo;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 这个类是：
 *
 * @author: MENG HU WANG
 * @date: 2025/2/5 18:50
 * @version: 1.0
 */

@RestController
@RequiredArgsConstructor
@RequestMapping("purchase")
public class PurchaseController {

    private final BuyListService buyListService;

    // 采购订单分页查询
    @GetMapping("purchase-page-list")
    public R purchasePageList(@RequestParam(defaultValue = "1") int pageNum,
                              @RequestParam(defaultValue = "5") int pageSize,
                              BuyListDTO buyListDTO) {
        PageInfo<BuyListVo> list = buyListService.purchasePageList(pageNum, pageSize, buyListDTO);
        return R.ok(list);
    }

    // 添加采购单
    @PostMapping("purchase-add")
    public R purchaseAdd(@RequestBody BuyListAddDTO buyListAddDTO) {
        buyListService.saveBuyList(buyListAddDTO);
        return R.ok("采购单添加成功！");
    }

    // 修改采购单
    @PutMapping("purchase-update")
    public R purchaseUpdate(@RequestBody BuyListUpdateDTO updateDTO) {
        buyListService.updateBuyList(updateDTO);
        return R.ok("采购单修改成功！");
    }

    // 删除采购单
    @DeleteMapping("purchase-delete/{id}")
    public R purchaseDelete(@PathVariable Integer id) {
        buyListService.deleteBuyList(id);
        return R.ok("采购单删除成功！");
    }

    // 生成入库单
    @PostMapping("in-warehouse-record-add")
    public R inWarehouseRecordAdd(@RequestBody BuyListInStoreDTO buyListInStoreDTO) {
        buyListService.saveInWarehouseRecord(buyListInStoreDTO);
        return R.ok("入库单添加成功！");
    }

    // 导出采购单
    @GetMapping("/exportTable")
    public R exportTable(BuyListQueryDTO queryDTO) {
        List<BuyListExportVO> exportData = buyListService.exportTable(queryDTO);
        return R.ok("导出成功", exportData);
    }

}
